package com.liukai.bishe.ui.activity.userInfo;

import androidx.appcompat.app.AppCompatActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.liukai.bishe.R;
import com.liukai.bishe.model.doman.user.User;
import com.liukai.bishe.model.doman.user.UserMessage;
import com.liukai.bishe.presenter.userPresenter.IUpdateUserInfoPresenter;
import com.liukai.bishe.presenter.userPresenter.impl.IUpdateUserInfoPresenterImpl;
import com.liukai.bishe.utils.Constants;
import com.liukai.bishe.utils.ToastUtil;
import com.liukai.bishe.view.IUpdateUserInfoCallBack;

public class ChangePasswordActivity extends AppCompatActivity implements View.OnClickListener,IUpdateUserInfoCallBack {

    private Unbinder mBind;

    private Context mContext = ChangePasswordActivity.this;

    @BindView(R.id.btnBack)
    public ImageView btnBack;

    @BindView(R.id.title)
    public TextView tvTitle;

    @BindView(R.id.btnSave)
    public TextView btnSave;

    @BindView(R.id.oldPsd)
    public EditText tvOldPsd;

    @BindView(R.id.showOldPsd)
    public ImageView btnShowOldPsd;

    @BindView(R.id.newPsw)
    public EditText tvNewPsw;

    @BindView(R.id.showNewPsd)
    public ImageView btnShowNewPsd;

    @BindView(R.id.crimPsw)
    public EditText tvCrimPsw;

    @BindView(R.id.showCrimPsd)
    public ImageView btnShowCrimPsd;


    private IUpdateUserInfoPresenter mIUpdateUserInfoPresenter;
    private Intent mIntent;
    private String mContent;
    private boolean isShowNewPsd = false;
    private boolean isShowOldPsd = false;
    private boolean isShowCrimPsd = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);

        mBind = ButterKnife.bind(this);
        initData();
        initListener();


    }

    private void initListener() {
        btnBack.setOnClickListener(this);
        tvTitle.setOnClickListener(this);
        tvCrimPsw.setOnClickListener(this);
        btnSave.setOnClickListener(this);
        btnShowCrimPsd.setOnClickListener(this);
        tvNewPsw.setOnClickListener(this);
        btnShowNewPsd.setOnClickListener(this);
        tvOldPsd.setOnClickListener(this);
        btnShowOldPsd.setOnClickListener(this);
    }

    private void initData() {

        mIntent = getIntent();
        String title = mIntent.getStringExtra("title");
        mContent = mIntent.getStringExtra("content");
        mIUpdateUserInfoPresenter = new IUpdateUserInfoPresenterImpl();
        mIUpdateUserInfoPresenter.registerCallback(this);
        tvTitle.setText("修改" + title);

        tvNewPsw.setTransformationMethod(PasswordTransformationMethod.getInstance());//显示
        tvOldPsd.setTransformationMethod(PasswordTransformationMethod.getInstance());//显示
        tvCrimPsw.setTransformationMethod(PasswordTransformationMethod.getInstance());//显示

    }


    @Override
    public void onClick(View view) {

        switch (view.getId()){

            case R.id.btnBack : // 返回
                finish();
                break;

            case R.id.title : // 标题

                break;

            case R.id.btnSave : // 保存

                String oldPsw = tvOldPsd.getText().toString();
                String newPsw = tvNewPsw.getText().toString();
                String crmPsw = tvCrimPsw.getText().toString();

                if (oldPsw.equals("")){
                    ToastUtil.show(mContext,"请输入旧密码!");
                }else if(!oldPsw.equals(mContent)){
                    ToastUtil.show(mContext,"旧密码错误!");
                }else{
                    if(newPsw.equals("") || crmPsw.equals("")){
                        ToastUtil.show(mContext,"新密码和确认密码不能为空!");
                    }else if(!newPsw.equals(crmPsw)){
                        ToastUtil.show(mContext,"两次密码不一致!");
                    }else{
                        save();
                    }
                }

                break;

            case R.id.showNewPsd :
                if(!isShowNewPsd){
                    tvNewPsw.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//显示
                    btnShowNewPsd.setImageResource(R.mipmap.open_eyes);
                    isShowNewPsd = true;
                }else{
                    tvNewPsw.setTransformationMethod(PasswordTransformationMethod.getInstance());//隐藏
                    btnShowNewPsd.setImageResource(R.mipmap.close_eyes);
                    isShowNewPsd = false;
                }
                break;

            case R.id.showOldPsd :
                if(!isShowOldPsd){
                    tvOldPsd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//显示
                    btnShowOldPsd.setImageResource(R.mipmap.open_eyes);
                    isShowOldPsd = true;
                }else{
                    tvOldPsd.setTransformationMethod(PasswordTransformationMethod.getInstance());//隐藏
                    btnShowOldPsd.setImageResource(R.mipmap.close_eyes);
                    isShowOldPsd = false;
                }
                break;

            case R.id.showCrimPsd :
                if(!isShowCrimPsd){
                    tvCrimPsw.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//显示
                    btnShowCrimPsd.setImageResource(R.mipmap.open_eyes);
                    isShowCrimPsd = true;
                }else{
                    tvCrimPsw.setTransformationMethod(PasswordTransformationMethod.getInstance());//隐藏
                    btnShowCrimPsd.setImageResource(R.mipmap.close_eyes);
                    isShowCrimPsd = false;
                }
                break;

        }

    }

    private void save() {

        String newPsw = tvNewPsw.getText().toString();
        User user = Constants.getUserInfo();
        user.setPassword(newPsw);
        mIUpdateUserInfoPresenter.update(user);

    }

    @Override
    public void result(Object object) {
        UserMessage userMessage = (UserMessage)object; // 获取服务器的返回信息
        if (userMessage.getCode().equals("3001")){ // 修改成功
            mIntent.putExtra("userMessage",userMessage);
            setResult(RESULT_OK,mIntent);
//            User user = userMessage.getUser(); // 获取修改后的信息
//            Constants.setUser(user); //修改全局User
//            setResult(RESULT_OK,mIntent);
        }
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mBind != null) {
            mBind = null;
        }
    }
}
